package com.fg.cloud.agency.controller.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.agency.controller.BaseAgencyController;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.FacImages;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.factory.FactoryParamsEntity;
import com.fg.cloud.manager.entry.factory.FactoryPaymentEntity;
import com.fg.cloud.manager.service.factory.FactoryImagesService;
import com.fg.cloud.manager.service.factory.FactoryParamsService;
import com.fg.cloud.manager.service.factory.FactoryPaymentService;
import com.fg.cloud.manager.service.factory.LogisticsService;

/**
 * Description:厂商信息显示
 * 
 * @author shaoqiping
 * @date 2017年8月15日下午6:25:29
 */
@Controller
@RequestMapping("/agencyFac")
public class FactoryAgencyController extends BaseAgencyController {

	@Autowired
	private FactoryParamsService paramsService;

	@Autowired
	private FactoryImagesService imagesService;

	@Autowired
	private FactoryPaymentService paymentService;

	@Autowired
	private LogisticsService logisticsService;
	/**
	 * Description:二维码+logo显示
	 * 
	 * @param request
	 * @param response
	 * @author shaoqiping
	 * @date 2017年8月15日下午6:26:25
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("/staticParam")
	@ResponseBody
	public void staticParam(HttpServletRequest request, HttpServletResponse response) {
		if (getCurrentAgencyUser().getFacId().equals(null)) {
			doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			return ;
		}
		try {
			Map<String, Object> map =null;
			if (getCurrentAgencyUser().getAgeId() == null || LongUtils.isEmpty(
					getCurrentAgencyUser().getAgeId())) {
				map = imagesService.selectByFacId(getCurrentAgencyUser().getFacId(),FacImages.STATIC);
			}else{
				map = imagesService.selectByAgeId(getCurrentAgencyUser().getAgeId(), getCurrentAgencyUser().getFacId(),FacImages.STATIC);
			}
			doResult(response, true, StatusCode.FINDSUCCESS, map, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:轮播图+联系我们显示
	  * @param request
	  * @param response
	  * @author shaoqiping
	  * @date 2017年10月23日下午4:37:58
	*/
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("/moveParam")
	@ResponseBody
	public void moveParam(HttpServletRequest request, HttpServletResponse response) {
		if (getCurrentAgencyUser().getFacId().equals(null)) {
			doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			return ;
		}
		try {
			Map<String, Object> map =null;
			if (getCurrentAgencyUser().getAgeId() == null || LongUtils.isEmpty(
					getCurrentAgencyUser().getAgeId())) {
				map = imagesService.selectByFacId(getCurrentAgencyUser().getFacId(),FacImages.MOVE);
			}else{
				map = imagesService.selectByAgeId(getCurrentAgencyUser().getAgeId(), getCurrentAgencyUser().getFacId(),FacImages.MOVE);
			}
			FactoryParamsEntity paramsEntity = paramsService.selectByFacId(getCurrentAgencyUser().getFacId());
			map.put("params", paramsEntity);
			doResult(response, true, StatusCode.FINDSUCCESS, map, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商选择支付方式
	 * 
	 * @param request
	 * @param response
	 * @param paymentType
	 * @author shaoqiping
	 * @date 2017年8月15日下午5:48:36
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value = "/payment", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void selectByFacId(HttpServletRequest request, HttpServletResponse response, Integer paymentType) {
		try {
			if (paymentType == null || paymentType < 0) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择支付类型");
				return;
			}
			List<FactoryPaymentEntity> payments = paymentService.findByFacId(getCurrentAgencyUser().getFacId(),
					paymentType);
			doResult(response, true, StatusCode.FINDSUCCESS, payments, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:查询所有物流公司
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @author shaoqiping
	 * @date 2017年8月7日下午5:44:07
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value = "/findLogistics", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void findAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			doResult(response, true, StatusCode.FINDSUCCESS,
					logisticsService.findAll(getCurrentAgencyUser().getFacId()), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
